﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VCC.Model;
using VCC.Lib.Common;
using System.Text;
using VCC.Site.Properties;
using VCC.Site.Models;
using System.IO;

namespace VCC.Site.Controllers
{
    public class AjaxDataController : Controller
    {
        //
        // GET: /AjaxData/

        ChoVietDataContext eDataContext = new ChoVietDataContext();

        public void ChangeLanguage(int id)
        {
            if (id > 0)
            {
                Session.Remove("LanguageID");
                Session["LanguageID"] = id.ToString();
                
            }
            return ;
            
        }
        public CaptchaImageResult ShowCaptchaImage()
        {
            return new CaptchaImageResult();
        }
        public JsonResult ValidateCatcha()
        {
            try
            {
                if (Request["CaptchaText"].ToLower() == HttpContext.Session["captchastring"].ToString().ToLower())
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

        public string ShowExplainVipType(int id)
        {
            if (id == 1)
            {
                return Settings.Default.ExplainVipDacBiet.ToString();
            }
            else if (id == 2)
            {
                return Settings.Default.ExplainVip1.ToString();
            }
            else if (id == 3)
            {
                return Settings.Default.ExplainVip2.ToString();
            }
            else if (id == 4)
            {
                return Settings.Default.ExplainVip3.ToString();
            }
            else
            {
                return Settings.Default.ExplainTinThuong.ToString();
            }
        }

        #region SearchSuburb
        public JsonResult SearchSuburb()
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var term = Request["keyword"].ToString();
                var keywords = eDataContext.Suburbs
                    .Where(e => e.FullName.IndexOf(term) > -1)
                    .Take(20)
                    .ToList();
                return Json(keywords, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        public ActionResult UpdateShortNameSubur()
        {
            ChoVietDataContext ed = new ChoVietDataContext();
            List<Suburb> ls = ed.Suburbs.ToList();
            foreach (Suburb item in ls)
            {
                item.ShortName = VCC.Lib.Helpers.TextHelper.ToUrlFriendly(item.FullName);
            }
            ed.SubmitChanges();
            return View();
        }

        #region SearchTag
        public JsonResult SearchTag()
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var term = Request["keyword"].ToString();
                var keywords = eDataContext.Tags
                    .Where(e => e.Name.IndexOf(term) > -1)
                    .Take(20)
                    .ToList();
                return Json(keywords, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region SearchTag
        public  JsonResult LoadImageByAJAX()
        {
            
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                var temp = Request.Files;
                string name = Request["fb"];
                var s = Request["dtcontent"];
                if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    var pic = System.Web.HttpContext.Current.Request.Files["HelpSectionImages"];
                }
                return Json("true",JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("False",JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        [HttpPost]
        public JsonResult UploadImageTemp()
        {
            string result = string.Empty;
            if (Request.Files.Count > 0)
            {
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    if (file.ContentLength > 0)
                    {
                        string MidPath = string.Empty;
                        if(HttpContext.Session["UpImageTem"] != null && !string.IsNullOrEmpty(HttpContext.Session["UpImageTem"].ToString()))
                        {
                            MidPath = HttpContext.Session["UpImageTem"].ToString();
                        }
                        else{
                            MidPath = string.Format("{0:yyyy/MM/dd}", DateTime.UtcNow).Replace("/", "\\") + "\\" + Utils.RandomString(7) + "\\";
                            HttpContext.Session["UpImageTem"] = MidPath;
                        }
                        result = Utils.ReduceImageSize(file.FileName, Settings.Default.AdImageUploadTemp + MidPath, Settings.Default.SiteRootPath, Convert.ToInt32(Settings.Default.RedureSizePercent), file);
                    }
                }
            }
            return Json(result,JsonRequestBehavior.AllowGet);
        }
        public bool DeleteImageTemp()
        {
            string imgUrl = Request["ImgTUrl"]!= null ? Request["ImgTUrl"] : string.Empty;
            bool result = true;
            if (!string.IsNullOrEmpty(imgUrl))
            {
              result=  Uploads.DeleteFileImage(Settings.Default.SiteRootPath + imgUrl.Replace("/", "\\"));
            }
            return result;
        }
    }
}
